
package com.joysuch.wwyt.mh.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.mh.bean.MhGradingAlarmBean;
import com.joysuch.wwyt.mh.bean.MhMajorHazardChemProductRelListBean;
import com.joysuch.wwyt.mh.bean.MhMajorHazardListBean;
import com.joysuch.wwyt.mh.bean.vo.MajorHazardDeleteCheckVO;
import com.joysuch.wwyt.mh.entity.MhGradingAlarm;
import com.joysuch.wwyt.mh.entity.MhMajorHazardChemProduct;
import com.joysuch.wwyt.mh.service.MhGradingAlarmService;
import com.joysuch.wwyt.mh.service.MhMajorHazardService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/mh/major-hazard")
@Api(tags = "/mh/major-hazard 重大危险源")
public class MhMajorHazardController {

    @Autowired
    private MhMajorHazardService mhMajorHazardService;

    @Autowired
    private MhGradingAlarmService mhGradingAlarmService;

    @GetMapping("get")
    public ResultBean get(Pageable pageable) {
        Page<MhMajorHazardListBean> mhMajorHazardListBeans = mhMajorHazardService.get(pageable);
        return ResultBean.pageData(mhMajorHazardListBeans.getContent(), mhMajorHazardListBeans.getTotalElements());
    }

    @GetMapping("/list")
    public ResultBean list(Pageable page, String name, String levels) {
        Page<MhMajorHazardListBean> list = mhMajorHazardService.list(page, name, levels);
        return ResultBean.pageData(list.getContent(), list.getTotalElements());
    }

    @PostMapping("/save")
    public ResultBean add(@RequestBody @Valid MhMajorHazardChemProductRelListBean mhMajorHazardChemProductRelListBean) {
        mhMajorHazardService.add(mhMajorHazardChemProductRelListBean);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("gethcs")
    public ResultBean getChannelProduct(Long id) {
        MhMajorHazardChemProduct gethcs = mhMajorHazardService.gethcs(id);
        return ResultBean.wrapSuccessfulResult(gethcs);
    }

    @GetMapping("/getById")
    public ResultBean getById(Long id) {
        MhMajorHazardChemProductRelListBean byId = mhMajorHazardService.getById(id);
        return ResultBean.wrapSuccessfulResult(byId);
    }

    @PostMapping("/edit")
    public ResultBean edit(@RequestBody @Valid MhMajorHazardChemProductRelListBean mhMajorHazardChemProductRelListBean) {
        mhMajorHazardService.edit(mhMajorHazardChemProductRelListBean);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/delete")
    public ResultBean delete(Long id) {
        try {
            mhMajorHazardService.delete(id);
        } catch (ApiBusinessException e) {
            if (e.getCode() == Status.DELETE_CHECK_FAILED.getCode() && e.getMessage() != null) {
                List<MajorHazardDeleteCheckVO> list = Lists.newArrayList();
                MajorHazardDeleteCheckVO vo = JSONObject.parseObject(e.getMessage(), MajorHazardDeleteCheckVO.class);
                list.add(vo);
                return ResultBean.fail(e.getCode(), "", list);
            } else {
                return ResultBean.fail(e.getCode(), e.getMessage());
            }
        }
        return ResultBean.defaultSuccessResult();
    }


    @GetMapping("/getList")
    public ResultBean getList() {
        List<SimpleSelectListBean> list =mhMajorHazardService.getList();
        return ResultBean.success(list);
    }

    @PostMapping("/saveGradingAlarm")
    public ResultBean saveGradingAlarm(@RequestBody @Valid MhGradingAlarm mhGradingAlarm) {
        mhGradingAlarmService.saveMhGradingAlarm(mhGradingAlarm);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("/getGradingAlarm")
    public ResultBean getGradingAlarm() {
        MhGradingAlarmBean mhGradingAlarmByOrgCode = mhGradingAlarmService.getMhGradingAlarmByOrgCode(Context.getCurrentOrgCode());
        return ResultBean.success(mhGradingAlarmByOrgCode);
    }

    @GetMapping("/getTaskNum")
    public ResultBean getTaskNum(Long hazardId) {
        int taskNum = mhMajorHazardService.getTaskNum(hazardId);
        return ResultBean.success(taskNum);
    }


}
